package io.halo.payment.modules.app.controller;

import io.halo.payment.common.utils.R;
import io.halo.payment.common.validator.ValidatorUtils;
import io.halo.payment.modules.app.annotation.Login;
import io.halo.payment.modules.app.form.UserOrderForm;
import io.halo.payment.modules.app.model.HaloPaymentOrder;
import io.halo.payment.modules.app.service.HaloPaymentOrderService;
import io.halo.payment.modules.app.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * 订单
 *
 * @author: austin
 * @since: 2022/12/30 22:09
 */
@Slf4j
@RestController
@RequestMapping("/app/order")
@Api("订单业务相关接口")
public class HaloPaymentOrderController {

    @Autowired
    private HaloPaymentOrderService haloPaymentOrderService;

    @Autowired
    private JwtUtils jwtUtils;


    @Login
    @PostMapping(value = "/searchUserOrderList")
    @ApiOperation(value = "查询用户订单")
    public R searchUserOrderList(@RequestBody UserOrderForm form, @RequestHeader HashMap<String, Object> header) {
        ValidatorUtils.validateEntity(form);
        String token = String.valueOf(header.get("token"));
        Long userId = Long.valueOf(jwtUtils.getClaimByToken(token).getSubject());

        Integer current = form.getCurrent();
        Integer pageSize = form.getPageSize();
        // 分页起始值
        Integer start = (current - 1) * pageSize;

        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("start", start);
        map.put("pageSize", pageSize);
        List<HaloPaymentOrder> haloPaymentOrderList = haloPaymentOrderService.searchUserOrderList(map);
        return R.ok().put("orderEntityList", haloPaymentOrderList);
    }
}
